<template>
	<el-container>
		<el-header style="height: auto">
			<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
		</el-header>
		<el-header>
			<div class="left-panel">
				<el-button icon="el-icon-plus" type="primary" @click="open_dialog" />
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model.trim="reqParams.key" clearable placeholder="输入产品编号或名称" />
					<el-button icon="el-icon-search" type="primary" @click="search" />
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable
				ref="table"
				:api-obj="this.$API.mditem.page"
				:params="reqParams"
				:column="column"
				row-key="id"
				hideContextMenu>
				<!-- 固定列-选择列 -->
				<el-table-column label="#" type="index" width="50"></el-table-column>
				<el-table-column align="center" fixed="right" label="操作" width="140">
					<template #default="scope">
						<el-button text type="primary" size="small" @click="open_dialog(scope.row)"> 编辑 </el-button>
						<el-divider direction="vertical" />
						<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
							<template #reference>
								<el-button text type="primary" size="small">删除</el-button>
							</template>
						</el-popconfirm>
					</template>
				</el-table-column>
				<template #classify="{ data }">
					<el-tag v-if="data.classify == 1" :type="''" effect="dark">{{ data.classifyDesc }}</el-tag>
					<el-tag v-if="data.classify == 2" :type="'success'" effect="dark">{{ data.classifyDesc }}</el-tag>
					<el-tag v-if="data.classify == 3" :type="'info'" effect="dark">{{ data.classifyDesc }}</el-tag>
				</template>
			</scTable>
		</el-main>

		<modify ref="modify" @complete="complete" />
	</el-container>
</template>

<script>
import modify from "./modify";

export default {
	components: {
		modify,
	},
	data() {
		return {
			reqParams: {
				key: "",
				classify: 0,
			},
			filterData: [
				{
					title: "物料分类",
					key: "classify",
					options: [
						{ label: "全部", value: 0 },
					],
				},
			],
			column: [
				{ prop: "id", label: "id", width: 100, hide: true },
				{ prop: 'itemCode', label: '物料编号', width: 100 },
				{ prop: 'itemName', label: '物料名称', width: 100 },
				{ prop: 'model', label: '物料型号', width: 100 },
				{ prop: 'spec', label: '物料规格', width: 200 },
				{ prop: 'unit', label: '计量单位', width: 100 },
				{ prop: 'attrDesc', label: '物料属性', width: 100 },
				{ prop: 'classify', label: '物料分类', width: 100 },
				{ prop: 'rules', label: '条码校验规则', width: 200 },
				{ prop: 'expiration', label: '保质期（天）', width: 100 },
				{ prop: 'supplier', label: '物料供应商', width: 100 },
				{ prop: 'createTime', label: '创建时间', width: 140 },
			],
		};
	},
	mounted() {
		this.init();
	},
	methods: {
		complete() {
			this.$refs.table.refresh();
		},
		search() {
			this.$refs.table.refresh();
		},
		filterChange(data) {
			this.reqParams.classify = data.classify;
			this.$refs.table.refresh();
		},
		async init() {
			const resp1 = await this.$API.enumlist.model.get('materialClassifyEnum');
			const options = resp1.data.map(({ label, value }) => ({ label: label, value: value }));
			this.filterData[0].options.push(...options);
		},
		async table_del(row) {
			let resp = await this.$API.mditem.delete.delete([row.id]);
			if (resp.code == 200) {
				this.$refs.table.refresh();
				this.$message.success("删除成功");
			} else {
				this.$alert(resp.message, "提示", { type: "error" });
			}
		},
		open_dialog(row) {
			if (row.id) {
				this.$refs.modify.open(row);
			} else {
				this.$refs.modify.open();
			}
		},
	},
};
</script>
